Skip to content

catch rate-limiting errors and fix other error-parsing for api key validation#63

Merged
mrmauer merged 3 commits intomainfrom
rate-limit-error
Jan 22, 2025
Merged

catch rate-limiting errors and fix other error-parsing for api key validation#63
mrmauer merged 3 commits intomainfrom
rate-limit-error

Conversation

@mrmauer
Copy link
Contributor

@mrmauer mrmauer commented Jan 15, 2025

After PR

Before this PR, all error responses from the BE when validating an end-user api key were getting flattened into ApiKeyError::UnknownError. Now we catch the validation or rate limiting error with the data returned from the PropelAuth. Example use:

let result = auth.api_key().validate_api_key(ValidateApiKeyParams {
    api_key_token: key_to_check,
}).await;

match result {
    Ok(response) => {
        HttpResponse::Ok().json(serde_json::json!({
            "message": "Success!",
        }))
    }
    Err(ApiKeyError::RateLimited {
        wait_seconds, // an f64 of the required wait time in seconds before a request from the same key
        user_facing_error,
    }) => {
        HttpResponse::TooManyRequests().json(serde_json::json!({
            "message": user_facing_error,
            "wait_seconds": wait_seconds,
        }))
    }
    Err(ApiKeyError::InvalidAPIKey {
        message,
    }) => {
        HttpResponse::Unauthorized().json(serde_json::json!({
            "message": message,
        }))
    }
    Err(_) => {
        HttpResponse::InternalServerError().json(serde_json::json!({
            "message": "An unknown error occurred",
        }))
    }
}

Tests

Verified all expected behavior within an example application.

Note

See here for a description of all the possible BE error responses.

Copy link
Contributor

@pfvatterott pfvatterott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested yesterday, LGTM

@mrmauer mrmauer merged commit a99393d into main Jan 22, 2025
1 check passed
@mrmauer mrmauer deleted the rate-limit-error branch January 22, 2025 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants